package org.dromara.exam.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.exam.domain.vo.ExamHistoryPaperVo;
import org.dromara.exam.domain.bo.ExamHistoryPaperBo;
import org.dromara.exam.service.IExamHistoryPaperService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 历年题库预览
 *
 * @author Lion Li
 * @date 2025-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/exam/historyPaper")
public class ExamHistoryPaperController extends BaseController {

    private final IExamHistoryPaperService examHistoryPaperService;

    /**
     * 查询历年题库预览列表
     */
    @SaCheckPermission("exam:historyPaper:list")
    @GetMapping("/list")
    public TableDataInfo<ExamHistoryPaperVo> list(ExamHistoryPaperBo bo, PageQuery pageQuery) {
        return examHistoryPaperService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出历年题库预览列表
     */
    @SaCheckPermission("exam:historyPaper:export")
    @Log(title = "历年题库预览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamHistoryPaperBo bo, HttpServletResponse response) {
        List<ExamHistoryPaperVo> list = examHistoryPaperService.queryList(bo);
        ExcelUtil.exportExcel(list, "历年题库预览", ExamHistoryPaperVo.class, response);
    }

    /**
     * 获取历年题库预览详细信息
     *
     * @param paperId 主键
     */
    @SaCheckPermission("exam:historyPaper:query")
    @GetMapping("/{paperId}")
    public R<ExamHistoryPaperVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long paperId) {
        return R.ok(examHistoryPaperService.queryById(paperId));
    }

    /**
     * 新增历年题库预览
     */
    @SaCheckPermission("exam:historyPaper:add")
    @Log(title = "历年题库预览", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamHistoryPaperBo bo) {
        return toAjax(examHistoryPaperService.insertByBo(bo));
    }

    /**
     * 修改历年题库预览
     */
    @SaCheckPermission("exam:historyPaper:edit")
    @Log(title = "历年题库预览", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamHistoryPaperBo bo) {
        return toAjax(examHistoryPaperService.updateByBo(bo));
    }

    /**
     * 删除历年题库预览
     *
     * @param paperIds 主键串
     */
    @SaCheckPermission("exam:historyPaper:remove")
    @Log(title = "历年题库预览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paperIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] paperIds) {
        return toAjax(examHistoryPaperService.deleteWithValidByIds(List.of(paperIds), true));
    }
}
